import os, sys, requests, base64, time

from bs4 import BeautifulSoup
from tqdm import tqdm
from config import BASE_URL
from requests.adapters import HTTPAdapter

pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd + '../')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'riscv_build_dataset.settings')

import django

django.setup()

from obs.models import obs_job_history, obs_package, obs_project
from obs.spider import package_build_log_spider
from obs.spider.job_history_spider import save_job_history_to_db

OBS_username = os.environ.get('OBS_USERNAME')
OBS_password = os.environ.get('OBS_PASSWORD')
credential = OBS_username + ':' + OBS_password
b64credential = base64.b64encode(credential.encode("ascii")).decode("ascii")
# print(b64credential)

headers = {
    'Accept': 'application/xml',
    'charset': 'utf-8',
    'Authorization': 'Basic ' + b64credential,
}

session = requests.Session()
session.mount('https://', HTTPAdapter(max_retries=5))


def get_job_history(project, repository, arch):
    url = f'{BASE_URL}/build/{project}/{repository}/{arch}/_jobhistory'
    resp = session.get(url, headers=headers)
    print(resp.text)

    soup = BeautifulSoup(resp.text, 'html.parser')
    try:
        for i in soup.find_all('jobhist'):
            package = i.get('package')
            job = [{
                'package_revision': i.get('rev'),
                'build_time': i.get('readytime'),
                'build_reason': i.get('reason'),
                'build_status_code': i.get('code'),
                'build_duration': int(i.get('endtime')) - int(i.get('starttime')),
                'build_worker': i.get('workerid'),
                'versrel': i.get('versrel'),
                'srcmd5': i.get('srcmd5'),
            }]
            save_job_history_to_db(project, package, job)
            log = package_build_log_spider.get_log_file_from_url(project, repository, arch, package)
            package_build_log_spider.save_log_to_db(project, package, log)
    except Exception as e:
        print('Cannot find job history, skipping...', e, file=sys.stderr)


if __name__ == '__main__':
    project_name = os.environ.get('OBS_PROJECT')
    repository_name = os.environ.get('OBS_REPO')
    arch_name = os.environ.get('OBS_ARCH')

    get_job_history(project_name, repository_name, arch_name)
